﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.Calendar.RecurrenceRule;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindStyle.SPEntity.Attributes;

namespace WindStyle.SPCalendar
{
    public abstract class Event : SPEntity.Entity
    {
        [Field("Location")]
        public string Location { get; set; }

        [Field("EventDate")]
        public DateTime Start { get; set; }

        [Field("EndDate")]
        public DateTime End { get; set; }

        [Field("Description")]
        public string Description { get; set; }

        [Field("fAllDayEvent")]
        public bool IsAllDayEvent { get; set; }

        [Field("Category")]
        public string Category { get; set; }

        [Field("Workspace")]//TODO: update to LinkField later
        public string Workspace { get; set; }

        public bool IsRecurrence { get { return this.Type != EventType.Simple; } }

        public EventType Type
        {
            get
            {
                if (this is Event<Pattern.DailyByFrequencyPattern>)
                    return EventType.DailyByFrequency;
                else if (this is Event<Pattern.DailyByWeekdayPattern>)
                    return EventType.DailyByWeekday;
                else if (this is Event<Pattern.WeeklyPattern>)
                    return EventType.Weekly;
                else if (this is Event<Pattern.MonthlyByFrequencyPattern>)
                    return EventType.MonthlyByFrequency;
                else if (this is Event<Pattern.MonthlyByDayPattern>)
                    return EventType.MonthlyByDay;
                else if (this is Event<Pattern.YearlyByFrequencyPattern>)
                    return EventType.YearlyByFrequency;
                else if (this is Event<Pattern.YearlyByDayPattern>)
                    return EventType.YearlyByDay;
                else
                    return EventType.Simple;
            }
        }

        public static Event ParseEvent(SPListItem sourceItem, SPRegionalSettings regionalSettings)
        {
            Event result = null;

            var isRecurrence = sourceItem[SPBuiltInFieldId.fRecurrence] != null && bool.Parse(sourceItem[SPBuiltInFieldId.fRecurrence].ToString());
            if (!isRecurrence)
            {
                result = new Event<Pattern.SimplePattern>();
                result.Read(sourceItem, regionalSettings);
            }
            else
            {
                var recurrence = new RecurrenceRule(sourceItem[SPBuiltInFieldId.RecurrenceData].ToString());

                switch (recurrence.Type)
                {
                    case RecurrenceRule.RecurrenceType.Daily:
                        if (recurrence.IsWeekday)
                        {
                            result = new Event<Pattern.DailyByWeekdayPattern>();
                            result.Read(sourceItem, regionalSettings);
                        }
                        else
                        {
                            result = new Event<Pattern.DailyByFrequencyPattern>();
                            result.Read(sourceItem, regionalSettings);
                        }
                        break;
                    case RecurrenceRule.RecurrenceType.Weekly:
                        result = new Event<Pattern.WeeklyPattern>();
                        result.Read(sourceItem, regionalSettings);
                        break;
                    case RecurrenceRule.RecurrenceType.Monthly:
                        result = new Event<Pattern.MonthlyByFrequencyPattern>();
                        result.Read(sourceItem, regionalSettings);
                        break;
                    case RecurrenceRule.RecurrenceType.MonthlyByDay:
                        result = new Event<Pattern.MonthlyByDayPattern>();
                        result.Read(sourceItem, regionalSettings);
                        break;
                    case RecurrenceRule.RecurrenceType.Yearly:
                        result = new Event<Pattern.YearlyByFrequencyPattern>();
                        result.Read(sourceItem, regionalSettings);
                        break;
                    case RecurrenceRule.RecurrenceType.YearlyByDay:
                        result = new Event<Pattern.YearlyByDayPattern>();
                        result.Read(sourceItem, regionalSettings);
                        break;
                }
            }
            return result;
        }

        public static void ApplyEvent<T>(Event<T> evt, SPListItem targetItem, SPRegionalSettings regionalSettings)
            where T : Pattern.IPattern, new()
        {
            evt.Write(targetItem, regionalSettings);
        }
    }
}
